package cn.yikaojiuguo.student.ui.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.umeng.analytics.MobclickAgent;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by cfan_yang on 2015/12/7.
 */
public class BaseActivity extends FragmentActivity{

    /**
     * 当前页面标签
     */
    protected String mTag;
    /**
     * 上下文
     */
    protected Context mContext;
    /**
     * 该Activity是否在前台
     */
    private boolean isForeGround = false;
    /**
     * 该Activity是否已销毁
     */
    private boolean isAlive = false;

    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTag = this.getClass().getSimpleName();
        isAlive = true;
        mContext = this;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mProgressDialog = new ProgressDialog(this);
        EventBus.getDefault().register(this);
    }

    public void setProgressMsg(String msg) {
        if(mProgressDialog!=null && mProgressDialog.isShowing()) {
            mProgressDialog.setMessage(msg);
        }
    }

    public void showProgressDialog(String msg) {
        if(mProgressDialog!=null) {
            mProgressDialog.setMessage(msg);
            mProgressDialog.setCanceledOnTouchOutside(true);
            mProgressDialog.show();
        }
    }

    public void showProgressDialog() {
        if(mProgressDialog!=null) {
            mProgressDialog.setCanceledOnTouchOutside(true);
            mProgressDialog.show();
        }
    }

    public void showProgressDialogNotCancel() {
        if(mProgressDialog!=null) {
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();
        }
    }

    public void showProgressDialogNotCancel(String msg) {
        if(mProgressDialog!=null) {
            mProgressDialog.setMessage(msg);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();
        }
    }

    public void dismissProgressDialog() {
        if(mProgressDialog!=null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        //标记为前台
        isForeGround = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        //标记为后台
        isForeGround = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isAlive = false;
        // 取消所有的网络请求
        RequestUtil.cancelRequest(mTag);
        EventBus.getDefault().unregister(this);
    }

    /**
     * 该Activity是否在前台
     */
    public boolean isForeGround() {
        return isForeGround;
    }

    /**
     * 该Activity是否已销毁
     */
    public boolean isAlive() {
        return isAlive;
    }

    /**
     * 启动Activity从左侧进入
     */
    public void leftEnter() {
        overridePendingTransition(R.anim.anim_previous_enter,R.anim.anim_previous_exit);
    }

    /**
     * 启动Activity从右侧进入
     */
    public void rightEnter() {
        overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
    }

    /**
     * 接收到消息时被调用，有需求的子类重写该方法
     */
    public void onEvent(MessageEvent event){
        if(MessageEvent.MessageType.DISMISS_PROGRESS_DIALOG == event.messageType) {
            dismissProgressDialog();
        } else if(MessageEvent.MessageType.FINISH == event.messageType) {
            if(TextUtils.isEmpty(event.message)) {
                finish();
            } else if(event.message.equals(this.getClass().getSimpleName())) {
                finish();
            }

        }
    }
}
